package com.erp.framework.controller.taglib;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;

import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class CustomMultiBoxTag extends javax.servlet.jsp.tagext.TagSupport {
    private String name = null;
    private String table = null;
    private String valuecolumn = null;
    private String textcolumn = null;
    private String where = null;
    private ArrayList whereValue;
    private String order = null;
    private String[] checked = null;
    private String onClick = null;
    private String id = null;

    public void setName(String text) {
        this.name = text;
    }

    public void setTable(String text) {
        this.table = text;
    }

    public void setValuecolumn(String text) {
        this.valuecolumn = text;
    }

    public void setTextcolumn(String text) {
        this.textcolumn = text;
    }

    public void setWhere(String text) {
        this.where = text;
    }

    public void setWhereValue(ArrayList whereValue) {
        this.whereValue = whereValue;
    }

    public void setOrder(String text) {
        this.order = text;
    }

    public void setChecked(String[] text) {
        this.checked = text;
    }

    public void setOnClick(String text) {
        this.onClick = text;
    }

    public void setId(String text) {
        this.id = text;
    }

    public int doEndTag() throws JspException {
        Connection conn = null;
        PreparedStatement pStmt = null;
        ResultSet rs = null;
        StringBuffer rslt = new StringBuffer();
        String query = "";

        try {
            query = " SELECT " + valuecolumn + " COLA," + textcolumn + " COLB FROM " + table
                    + ((where != null && !where.equals("")) ? " WHERE " + where : "")
                    + ((order != null && !order.equals("")) ? " ORDER BY " + order : "");

            String valueStr = null;
            String captionStr = null;
            conn = ServiceLocator.getInstance().getDSDBConnection();
            pStmt = conn.prepareStatement(query);

            Logger.all(this, "doEndTag(), sql=" + query);

            if (whereValue != null) {
                for (int i = 0; i < whereValue.size(); ++i) {
                    Object critVal = whereValue.get(i);
                    if (critVal instanceof String)
                        pStmt.setString(i + 1, (String) critVal);
                    else if (critVal instanceof BigDecimal)
                        pStmt.setBigDecimal(i + 1, (BigDecimal) critVal);
                    else if (critVal instanceof Integer)
                        pStmt.setInt(i + 1, ((Integer) critVal).intValue());
                    else if (critVal instanceof java.sql.Date)
                        pStmt.setDate(i + 1, (Date) critVal);
                    else if (critVal instanceof Timestamp)
                        pStmt.setTimestamp(i + 1, (Timestamp) critVal);
                    else
                        pStmt.setObject(i + 1, critVal);
                }
            }

            rs = pStmt.executeQuery();

            if (rs != null) {
                int counter = 0;
                while (rs.next()) {
                    valueStr = (rs.getString(1) == null) ? "" : rs.getString(1).trim();
                    captionStr = (rs.getString(2) == null) ? "" : rs.getString(2).trim();

                    rslt.append("<input type=\"checkbox\"");
                    rslt.append(" name=\"").append(name).append("\"");
                    rslt.append(" value=\"").append(CommonUtil.stringRpl(valueStr, "\"", "&quot;")).append("\"");
                    rslt.append(" caption=\"").append(CommonUtil.stringRpl(captionStr, "\"", "&quot;")).append("\"");
                    for (int i = 0; checked != null && i < checked.length; i++) {
                        if (valueStr.equals(checked[i].trim())) {
                            rslt.append(" checked");
                            break;
                        }
                    }
                    if (id != null && !id.equals("")) {
                        rslt.append(" id=\"").append(id).append("\"");
                    }
                    if (onClick != null && !onClick.equals("")) {
                        rslt.append(" onclick=\"").append(CommonUtil.stringRpl(onClick, "<COUNTER>", String.valueOf(counter)))
                                .append("\"");
                    }
                    rslt.append("> ");
                    rslt.append(captionStr);
                    rslt.append("<br>\n");

                    counter++;
                }
            }

            Logger.all(this, "doEndTag(), html=" + rslt.toString());
            pageContext.getOut().print(rslt.toString());
        } catch (Exception e) {
            Logger.logExceptions(this, e);
            Logger.error("doEndTag(), html=" + rslt.toString());
            Logger.error("doEndTag(), sql=" + query);
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pStmt != null)
                    pStmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }

        return EVAL_PAGE;
    }
}
